package com.haruhinav.controller;


import com.auth0.jwt.interfaces.DecodedJWT;
import com.haruhinav.Utils.JWTUtils;
import com.haruhinav.entity.User;
import com.haruhinav.entity.vo.ResultVO;
import com.haruhinav.service.UserService;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class UserController {
    @Resource
    private UserService userService;

//    根据uid查找user
    @GetMapping("/a/find")
    public Map<String,Object>  findUserById(@RequestParam("uid") int uid){
        return userService.findUserById(uid);
    }


    //从token获取用户
    @PostMapping("/a/verify")
    public Map<String,Object> findUserByToken(HttpServletRequest request){
//        String token = request.getHeader("token");
//        //这个action是被过滤后的，不用判断token是否为null
//        DecodedJWT verify = JWTUtils.verify(token);
//        int uid = Integer.parseInt(verify.getClaim("userid").asString());

        return userService.findUserById(JWTUtils.fromTokenGetUid(request));
    }
    @PostMapping("/e/insert")
    public ResultVO insertUser(@RequestBody User user){
//此处返回的是插入数据的主键 UserServer return user.getId()
        return userService.insertUser(user);
    }
//修改用户(不修改密码)
    @PostMapping("/a/update")
    public ResultVO updateUser(@RequestBody User user){
        //user.setId(JWTUtils.fromTokenGetUid(request));
        return  userService.updateUser(user);
    }
//    验证用户密码
    @PostMapping("/a/checkpswd")
    public Map<String ,String> checkUserPassword(@RequestBody User user){
        //int uid = JWTUtils.fromTokenGetUid(request);
        return userService.checkUserPassword(user.getId(),user.getPassword());
    }
    @PostMapping("/a/update-pswd")
    public Map<String,String> updateUserPswd(@RequestBody User user){
        //int uid = JWTUtils.fromTokenGetUid(request);
        return userService.updateUserPswd(user.getId(),user.getPassword());
    }
//    仅修改用户头像
    @PostMapping("/a/update/face")
    public ResultVO updateUserFace(@RequestBody User user){
        return userService.updateUserFace(user.getFace(), user.getId());
    }

}
